<?php

namespace app\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;

/**
 * Default controller for the `api` module
 */
class DefaultController extends Controller
{
    public $enableCsrfValidation = false;
    /***
     * 检测用户名是否注册过
     * url:/api/default/checkusername-exist
     */
    public function actionCheckusernameExist(){
        $username = Tools::GetSafeParam('username', "", 1);
        $model = \common\models\User::findOne(['username'=>$username]);
        $output = [];
        if(empty($model)){
            $output['exist'] = false;
        }else{
            $output['exist'] = true;
        }
        Tools::outputjson($output);
    }
    
    /***
     * 检测手机号是否注册过
     * url:/api/default/checkmobile-exist
     */
    public function actionCheckmobileExist(){
        $mobile = Tools::GetSafeParam('mobile', "", 1);
        $model = \common\models\User::findOne(['mobile'=>$mobile]);
        $output = [];
        if(empty($model)){
            $output['exist'] = false;
        }else{
            $output['exist'] = true;
        }
        Tools::outputjson($output);
    }
    
    /***
     * 用户登录
     * url:/api/default/userlogin
     */
    public function actionUserlogin(){
        $post = Yii::$app->request->get();
        if(!empty($post)){
            $username = $post['username'];
            $passwd = $post['passwd'];
            $returnurl = $post['return_url'];
            $user = null;
            if(preg_match("/^1[23456789]{1}\d{9}$/",$username)){
                $user = \common\models\User::findOne(['mobile'=>$username]);
            }else{
                $user = \common\models\User::findOne(['username'=>$username]);
            }
            if(!$user || !$user->validatePassword($passwd)){
                $output['success'] = 0;
                $output['msg'] = "账号密码错误！";
                Tools::outputjson($output);
            }else{
                $login = Yii::$app->user->login($user, 3600 * 24 * 30);
                $service = \common\models\WebsocketSever2user::getServerByUserid($user->id);//websocket服务器地址
                
                if($user->usercode == ""){
                    $user->usercode = Tools::getUserCode($user->created_at, $user->id);
                    $user->save();
                }
                
                if($login){
                    return $this->redirect($returnurl);
//                    $output['success'] = 1;
//                    $output['server'] = $service;
//                    $output['isGuest'] = Yii::$app->user->isGuest;
//                    $output['username'] = Yii::$app->user->identity->username;
//                    
//                    
//                    $output['login'] = $login;
//
//                    $output['msg'] = "登录成功！";
//                    Tools::outputjson($output);
                }
                
            }
        }
    }
    
    public function actionValidpasswd(){
            $username = Tools::GetSafeParam('username', "", 1);
            $passwd = Tools::GetSafeParam('passwd', "", 1);
            $user = null;
            if(preg_match("/^1[23456789]{1}\d{9}$/",$username)){
                $user = \common\models\User::findOne(['mobile'=>$username]);
            }else{
                $user = \common\models\User::findOne(['username'=>$username]);
            }
            if(!$user || !$user->validatePassword($passwd)){
                $output['success'] = 0;
                $output['msg'] = "账号密码错误！";
            }else{
                $output['success'] = 1;
                $output['msg'] = "校验成功！";
            }
            Tools::outputjson($output);
    }


    public function actionCheckcode(){
        $code = Tools::GetSafeParam('code', "", 1);
        $hashcode = Tools::GetSafeParam('hashcode', "", 1);
        $string = Tools::decrypt($hashcode, Yii::$app->params['sms_encrypt_key']);
        $decrypt_arr = explode('_', $string);
        
        $data = [];
        if($decrypt_arr[1] == $code){
            $data['success'] = 1;
            $data['msg'] = "校验成功！";
            
        }else{
            $data['success'] = 0;
            $data['msg'] = "验证码错误！";
        }
        Tools::outputjson($data);
    }
}
